package com.example.competionmanagebackend.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.competionmanagebackend.entity.Users;
import com.example.competionmanagebackend.mapper.UsersMapper;
import com.example.competionmanagebackend.service.UsersService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UsersServiceImpl extends ServiceImpl<UsersMapper, Users>
        implements UsersService {

    @Resource
    private UsersMapper usersMapper;

    @Override
    public Users login(String userName, String password) {
        List<Users> users = usersMapper.selectByUserNameAndPassword(userName, password);
        return users.isEmpty() ? null : users.get(0);
    }

    @Override
    public Users getUserByUsername(String username) {
        return this.lambdaQuery().eq(Users::getUserName, username).one();
    }

    @Override
    public Page<Users> getUsersPage(int currentPage, int pageSize) {
        Page<Users> page = new Page<>(currentPage, pageSize);
        return this.page(page);
    }

    @Override
    public List<Users> getAllUsers() {
        return this.list();
    }

    @Override
    public void insertUser(Users user) {
        usersMapper.insertUser(user);
    }

    @Override
    public void updateUser(Users user) {
        usersMapper.updateUser(user);
    }

    @Override
    public void deleteUser(Integer id) {
        usersMapper.deleteUserById(id);
    }
}